﻿using System;
using System.Linq;
using www.Core.Contract.DataContract;
using www.Models;

namespace www.Data.DAO
{
    public class VideoRefDAO
    {
        #region value
        private RelaxDataContext _context = new RelaxDataContext();
        #endregion

        #region Hàm lấy dữ liệu

        public IQueryable<RelaxVideoDataContract> GetVideoDataContracts()
        {
            var lReturn = (from t in _context.VIDEO_REFs
                          join v in _context.VIDEO_VALUEs on t.id equals v.videoid
                          //join c in _context.CATEGORY_VALUEs on t.categoryId equals c.categoryid
                join l in _context.RelaxLogins on t.userId equals l.id
                          select new RelaxVideoDataContract
                          {
                              Id = Convert.ToInt32(t.id),
                              Name = v.name,
                              Tag = v.tag,
                              Active = Convert.ToInt32(t.active),
                              Date = Convert.ToDateTime(t.date ?? DateTime.Now),
                              Priority = Convert.ToInt32(t.priority ?? 0),
                              CategoryId = Convert.ToInt32(t.categoryId ?? 0),
                              //CategoryName = c.name,
                              View = Convert.ToInt32(t.view ?? 0),
                              Userid = Convert.ToInt32(t.userId ?? 0),
                              UserName = l.username,
                              IsDefault = Convert.ToInt32(t.isdefault ?? 0),
                              Code = v.link,
                              Languagecode = Convert.ToInt32(v.languagecode),
                              Report = Convert.ToInt32(v.report ?? 0),
                              UserBrower = Convert.ToInt32(t.userbrower ?? 0)
                          }).Distinct();
            return lReturn;
        }
        public IQueryable<RelaxVideoDataContract> GetVideoDataContract()
        {
            var lReturn = from t in _context.VIDEO_REFs
                          select new RelaxVideoDataContract
                          {
                              Id = Convert.ToInt32(t.id),
                              Active = Convert.ToInt32(t.active),
                              Date = Convert.ToDateTime(t.date ?? DateTime.Now),
                              Priority = Convert.ToInt32(t.priority ?? 0),
                              CategoryId = Convert.ToInt32(t.categoryId ?? 0),
                              View = Convert.ToInt32(t.view ?? 0),
                              Userid = Convert.ToInt32(t.userId ?? 0),
                              IsDefault = Convert.ToInt32(t.isdefault ?? 0)
                          };
            return lReturn;
        }
        #endregion
        #region Add
        public void Add(VIDEO_REF videoRef)
        {
            _context.VIDEO_REFs.InsertOnSubmit(videoRef);
            _context.SubmitChanges();

        }
        #endregion
        #region Update

        public void Update(VIDEO_REF videoRef)
        {
            var first = _context.VIDEO_REFs.FirstOrDefault(p => p.id == videoRef.id);
            if (first != null)
            {
                first.priority = videoRef.priority;
                first.active = videoRef.active;
                first.isdefault = videoRef.isdefault;
                first.categoryId = videoRef.categoryId;
                first.userId = videoRef.userId;
                first.userbrower = videoRef.userbrower;
                first.date = videoRef.date;
                _context.SubmitChanges();
            }
        }
        public void UpdateView(long id)
        {
            var first = _context.VIDEO_REFs.FirstOrDefault(p => p.id == id);
            if (first != null)
            {
                first.view = first.view+1;
                _context.SubmitChanges();
            }
        }
        #endregion

        #region Delete
        public void Delete(long iId)
        {
            var first = _context.VIDEO_REFs.FirstOrDefault(p => p.id == iId);
            if (first != null)
            {
                _context.VIDEO_REFs.DeleteOnSubmit(first);
                _context.SubmitChanges();
            }
        }
        #endregion
    }
}